from ckeditor.fields import RichTextField
from django.contrib.auth.models import AbstractUser
from django.db import models


# Create your models here.
class Site(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField()
    logo = models.ImageField(upload_to="site/logo/")

    class Meta:
        verbose_name = "site"
        verbose_name_plural = "1. Сайт"

    def __str__(self) -> str:
        return self.name


class User(AbstractUser):
    avatar = models.ImageField(
        upload_to="users/avatars/%Y/%m/%d/", default="users/avatars/no_image.svg"
    )
    bio = models.TextField(max_length=500, null=True)
    location = models.CharField(max_length=30, null=True)
    website = models.CharField(max_length=100, null=True)
    joined_date = models.DateField(auto_now_add=True)

    class Meta:
        verbose_name = "user"
        verbose_name_plural = "2. Пользователи"

    def __str__(self) -> str:
        return self.username


class Category(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField()
    description = models.TextField()

    class Meta:
        verbose_name = "category"
        verbose_name_plural = "3. Категории"

    def __str__(self) -> str:
        return self.name


class Tag(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField()
    description = models.TextField()

    class Meta:
        verbose_name = "tag"
        verbose_name_plural = "4. Теги"

    def __str__(self) -> str:
        return self.name


class Post(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField()
    content = RichTextField()
    featured_image = models.ImageField(upload_to="posts/featured_images/%Y/%m/%d/")
    is_published = models.BooleanField()
    is_featured = models.BooleanField()
    created_at = models.DateField(auto_now_add=True)
    modified_at = models.DateField(auto_now=True)

    likes = models.ManyToManyField(User, related_name="post_like")

    category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
    tag = models.ManyToManyField(Tag)
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)

    class Meta:
        verbose_name = "post"
        verbose_name_plural = "5. Посты"

    def __str__(self) -> str:
        return self.title

    def get_number_of_likes(self):
        return self.likes.count()


class Comment(models.Model):
    content = models.TextField(max_length=1000)
    created_at = models.DateField(auto_now_add=True)
    is_approved = models.BooleanField(default=False)

    likes = models.ManyToManyField(User, related_name="comment_like")

    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    post = models.ForeignKey(Post, on_delete=models.SET_NULL, null=True)

    class Meta:
        verbose_name = "comment"
        verbose_name_plural = "6. Коментарии"

    def __str__(self) -> str:
        if len(self.content) > 50:
            comment = self.content[:50] + "..."
        else:
            comment = self.content
        return comment

    def get_number_of_likes(self):
        return self.likes.count()
